#!/bin/bash
first(){
read -p "请输入xml配置文件名称: " num1
cp /etc/libvirt/qemu/`ls /etc/libvirt/qemu/ |grep ^moban` /etc/libvirt/qemu/$num1.xml&>/dev/null
read -p "请输入虚拟机名称: " num2
sed -i "s/vmname/$num2/" /etc/libvirt/qemu/$num1.xml&>/dev/null
#uuid名称
sed -i "s/vmuuid/`uuidgen`/" /etc/libvirt/qemu/$num1.xml&>/dev/null
read -p "请输入内存大小: " num4
sed -i "s/vmmem/$num4/" /etc/libvirt/qemu/$num1.xml&>/dev/null
read -p "请输入cpu内核数: " num5
sed -i "s/vmcpu/$num5/" /etc/libvirt/qemu/$num1.xml&>/dev/null
read -p "请输入磁盘路径名称: " num6
sed -i "s/vmdisk/$num6/" /etc/libvirt/qemu/$num1.xml&>/dev/null
qemu-img create -f qcow2 /var/lib/libvirt/images/$num6.qcow2 2G&>/dev/null
#网卡mac地址
sed -i "s/vmmac/`openssl rand -hex 3|sed 's/\(..\)/\1:/g; s/.$//'`/" /etc/libvirt/qemu/$num1.xml&>/dev/null
#激活启动
virsh define /etc/libvirt/qemu/$num1.xml&>/dev/null
virsh start $num1&>/dev/null
}
second(){
read -p "请输入创建虚拟机数量: " i
for count in `seq 1 $i`
do
#name
read -p "请输入xml配置文件名称: " num1
cp /etc/libvirt/qemu/`ls /etc/libvirt/qemu/ |grep ^moban` /etc/libvirt/qemu/$num1.xml&>/dev/null
read -p "请输入虚拟机名称: " num2
sed -i "s/vmname/$num2/" /etc/libvirt/qemu/$num1.xml&>/dev/null
#uuid
sed -i "s/vmuuid/`uuidgen`/" /etc/libvirt/qemu/$num1.xml&>/dev/null
#mem
read -p "请输入内存大小: " num3
sed -i "s/vmmem/$num3/" /etc/libvirt/qemu/$num1.xml&>/dev/null
#cpu
read -p "请输入cpu内核数: " num4
sed -i "s/vmcpu/$num4/" /etc/libvirt/qemu/$num4.xml&>/dev/null
#disk
read -p "请输入磁盘路径名称: " num5
sed -i "s/vmdisk/$num5/" /etc/libvirt/qemu/$num1.xml&>/dev/null
qemu-img create -f qcow2 /var/lib/libvirt/images/$num5.qcow2 2G&>/dev/null
#网卡mac地址
sed -i "s/vmmac/`openssl rand -hex 3|sed 's/\(..\)/\1:/g; s/.$//'`/" /etc/libvirt/qemu/$num1.xml&>/dev/null
#激活启动
virsh define /etc/libvirt/qemu/$num1.xml&>/dev/null
virsh start $num1&>/dev/null
done
}
third(){
read -p "请输入创建虚拟机数量: " i
for count in `seq 1 $i`
do
vmn=`echo "$RANDOM"`&>/dev/null
#name
cp /etc/libvirt/qemu/`ls /etc/libvirt/qemu/ |grep ^moban` /etc/libvirt/qemu/vm$vmn.xml&>/dev/null
sed -i "s/vmname/vm$vmn/" /etc/libvirt/qemu/vm$vmn.xml&>/dev/null
#uuid
sed -i "s/vmuuid/`uuidgen`/" /etc/libvirt/qemu/vm$vmn.xml&>/dev/null
#mem
sed -i "s/vmmem/2000000/" /etc/libvirt/qemu/vm$vmn.xml&>/dev/null
#cpu
sed -i "s/vmcpu/2/" /etc/libvirt/qemu/vm$vmn.xml&>/dev/null
#disk
sed -i "s/vmdisk/vm$vmn/" /etc/libvirt/qemu/vm$vmn.xml&>/dev/null
qemu-img create -f qcow2 /var/lib/libvirt/images/vm$vmn.qcow2 1G&>/dev/null
#网卡mac地址
sed -i "s/vmmac/`openssl rand -hex 3|sed 's/\(..\)/\1:/g; s/.$//'`/" /etc/libvirt/qemu/vm$vmn.xml&>/dev/null
#激活启动
virsh define /etc/libvirt/qemu/vm$vmn.xml&>/dev/null
virsh start vm$vmn&>/dev/null
done
}
fourth(){
echo "kvm主机名称"
virsh list --all --name
read -p "请输入你想删除的虚拟机: " n
virsh destroy $n &>/dev/null
virsh undefine $n &>/dev/null
rm -r /var/lib/libvirt/images/$n.qcow2&>/dev/null
if [ $? -eq 0 ]
then 
echo "$n删除成功"
else
echo "无法删除该虚拟机"
fi
}
fifth(){
echo "kvm主机名称"
virsh list --all --name
read -p "请输入你想查看的虚拟机: " n
virsh dominfo $n
}
sixth(){
pass
}
login(){
echo "**************************"
echo "1.创建自定义配置单个虚拟机"
echo "2.批量创建自定义配置虚拟机"
echo "3.批量创建默认配置虚拟机"
echo "4.删除虚拟机"
echo "5.查看虚拟机的配置"
echo "6.升配/降配"
echo "**************************"
read -p "请输入你的选择：" n
case $n in
1);;2);;3);;4);;5);;6);;
esac
}
login  
if [ $n -eq 1 ] 
then
first
elif [ $n -eq 2 ]
then
second
elif [ $n -eq 3 ]
then
third
elif [ $n -eq 4 ]
then
fourth
elif [ $n -eq 5 ]
then
fifth
else [ $n -eq 6 ]
pass
fi
